<?php

namespace app\admin\controller\microend;

use app\common\controller\Backend;
use think\Db;

/**
 * 商家店铺管理
 *
 * @icon fa fa-circle-o
 */
class Shop extends Backend
{

    /**
     * Shop模型对象
     * @var \app\common\model\Shop
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'shop_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Shop;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
     /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //
            // $join[] = ["admin a", "a.id = re.admin_id"];
            // $join[] = ["auth_group_access ac", "a.id = ac.uid"];
            // $join[] = ["auth_group g", "g.id = ac.group_id"];
            //$join[] = ["file f", "f.id = s.file_id"];
             $join[] = ['sd_admin s', 's.id = a.shop_admin_id'];
            $field  = "a.*, shop_id as id,s.username";
            $total  = $this->model->alias("a")->join($join)->field($field)->where($where)->count();
            $list   = $this->model->alias("a")->join($join)->field($field)->where($where)->limit($offset, $limit)->order("a.create_time desc")->select();
            //dump($this->model->getLastSql());
            $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
            foreach ($list as $key => $value) {
//                if($value['status'] == 0){
//                    $list[$key]['status'] = '待审核';
//                }else if($value['status'] == 1){
//                    $list[$key]['status'] = '审核通过';
//                }else{
//                    $list[$key]['status'] = '审核失败';
//                }
                $value["a.status"] = $value["status"];
                $area = explode(',', $value['area_id']);
                $area = array_pop($area);
                $value['area_id']   = Db::name('area')->where('id', $area)->value('mergename');
                $value['fullurl'] = $this->view->config['upload']['cdnurl'] . $value['shop_pic'];
                //$value['area_id'] = Db::name('area')->where('id',end(explode(',', $value['area_id'])))->value('mergename');
                //$value['shop_type'] = $value['shop_type'] == 0 ? '不限' : ($value['shop_type'] == 1 ? '天猫'  : '淘宝');
//                $value['mobile'] =
                $value["a.mobile"] = $value["mobile"];
                $value['a.shop_type'] = $value['shop_type'];
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function pass($ids=NULL)
    {
        //$row = $this->request->post("row/a");
        $row = $this->model->get(['shop_id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $order_paike_gere_bean =  Db::name("system")->where("key","order_paike_gere_bean")->value("value");
        $order_paike_first_reward = Db::name("system")->where("key","order_paike_first_reward")->value("value");
        $order_paike_second_reward = Db::name("system")->where("key","order_paike_second_reward")->value("value");
//            $order_paike_comment_review = Db::name("system")->where("key","order_paike_comment_review")->value("value");
//            $order_paike_comment_review_pic = Db::name("system")->where("key","order_paike_comment_review_pic")->value("value");
        $is_praise = Db::name("system")->where("key","sys_open_geredan_praise")->value("value");
        $min_limit = 0.00;
        if($is_praise==1){
            $min_limit = floatval($order_paike_gere_bean)+floatval($order_paike_first_reward)+floatval($order_paike_second_reward);
        }else{
            $min_limit = floatval($order_paike_gere_bean);
        }


        $order_paike_jindan_bean =  floatval(Db::name("system")->where("key","order_paike_jindan_bean")->value("value"));
        $is_praise_jin = Db::name("system")->where("key","sys_open_jinshuaidan_praise")->value("value");
        $min_limit_jing = 0.00;
        if($is_praise_jin==1) {
            $min_limit_jing = floatval($order_paike_jindan_bean) + floatval($order_paike_first_reward) + floatval($order_paike_second_reward);
        }else{
            $min_limit_jing = floatval($order_paike_jindan_bean);
        }
        //$min_limit_jing = floatval($order_paike_jindan_bean)+floatval($order_paike_first_reward)+floatval($order_paike_second_reward);
        if($this->request->isPost()){
            $params = $this->request->post("row/a");
//            $order_paike_gere_bean =  Db::name("system")->where("key","order_paike_gere_bean")->value("value");
////            $order_paike_comment = Db::name("system")->where("key","order_paike_comment")->value("value");
////            $order_paike_comment_review = Db::name("system")->where("key","order_paike_comment_review")->value("value");
////            $order_paike_comment_review_pic = Db::name("system")->where("key","order_paike_comment_review_pic")->value("value");
//            $min_limit = floatval($order_paike_gere_bean);
//
//            $order_paike_jindan_bean =  floatval(Db::name("system")->where("key","order_paike_jindan_bean")->value("value"));

            if($params["gold_bean"]<$min_limit){
                $this->error('隔日单金豆数不能小于系统设置最低额'.$min_limit);
            }
            if($params["jing_bean"]<$min_limit_jing){
                $this->error('精刷单单金豆数不能小于系统设置最低额'.$min_limit_jing);
            }
            if($params["limit_num"]<0){//
                $this->error('任务单数限制不能小于0');
            }
            if($params["status"] == 1){
                $this->error('该店铺已经通过审核请勿重复审核');
            }
            if($params["status"] == 2){
                $this->error('该店铺已经驳回，请商户重新发起申请');
            }
            $params['status'] = 1;
            $params['left_num'] = $params['limit_num'];
            $params['paike_gere'] =  $order_paike_gere_bean;
            $params['paike_jingdan'] =  $order_paike_jindan_bean;

            if($is_praise==1) {
                $params['paike_geredan_praise_first'] = floatval($order_paike_first_reward);
                $params['paike_geredan_praise_sec'] = floatval($order_paike_second_reward);
            }else{
                $params['paike_geredan_praise_first'] = 0.00;
                $params['paike_geredan_praise_sec'] = 0.00;
            }

            if($is_praise_jin==1){
                $params['paike_jingdan_praise_first'] = floatval($order_paike_first_reward);
                $params['paike_jingdan_praise_sec'] =  floatval($order_paike_second_reward);
            }else{
                $params['paike_jingdan_praise_first'] = 0.00;
                $params['paike_jingdan_praise_sec'] = 0.00;
            }

            try {
                $this->model->where('shop_id', $ids)->setField($params);
                $this->success('审核通过');
            } catch (Exception $e) {
                $this->error('审核失败');
            }

        }
        $this->view->assign("min_limit",$min_limit);
        $this->view->assign("min_limit_jing",$min_limit_jing);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function refuse()
    {
        //echo $ids;exit;
        $row = $this->request->post("row/a");
        //halt($row);
        if($this->request->isAjax()){
            $row = $this->model->get(['shop_id' => $row["shop_id"]]);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            try {
                $this->model->where(['shop_id' => $row["shop_id"]])->update(['status' => 2]);
                return json(['code' => 1, 'msg' => '驳回成功']);
                //dump($row);die;
            } catch (Exception $e) {
                return json(['code' => 0, 'msg' => '驳回失败']);
            }
        } else {
            return json(['code' => 0, 'msg' => '驳回失败']);
        }

    }

    public function edit($ids = '')
    {
        $row = $this->model->get(['shop_id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if($row->status != 1){
            $this->error('该店铺状态异常。不可编辑');
        }

        $order_paike_gere_bean =  Db::name("system")->where("key","order_paike_gere_bean")->value("value");
        $order_paike_first_reward = Db::name("system")->where("key","order_paike_first_reward")->value("value");
        $order_paike_second_reward = Db::name("system")->where("key","order_paike_second_reward")->value("value");
        $is_praise = Db::name("system")->where("key","sys_open_geredan_praise")->value("value");
        $min_limit = 0.00;
        if($is_praise==1){
            $min_limit = floatval($order_paike_gere_bean)+floatval($order_paike_first_reward)+floatval($order_paike_second_reward);
        }else{
            $min_limit = floatval($order_paike_gere_bean);
        }

        $order_paike_jingsha_bean =  Db::name("system")->where("key","order_paike_jindan_bean")->value("value");
        $is_praise_jin = Db::name("system")->where("key","sys_open_jinshuaidan_praise")->value("value");
        $min_limit_jing = 0.00;
        if($is_praise_jin==1) {
            $min_limit_jing = floatval($order_paike_jingsha_bean) + floatval($order_paike_first_reward) + floatval($order_paike_second_reward);
        }else{
            $min_limit_jing = floatval($order_paike_jingsha_bean);
        }

        if($this->request->isPost()){
            $params = $this->request->post("row/a");
            if($params["limit_num"]<$row["limit_num"]){//
                $this->error('修改的任务单数限制不能小于原单数限制');
            }
            if($params["gold_bean"]<$min_limit){
                $this->error('隔日单金豆数不能小于系统设置最低额'.$min_limit);
            }
            if($params["jing_bean"]<$min_limit_jing){
                $this->error('精刷单金豆数不能小于系统设置最低额'.$min_limit_jing);
            }
            $shop_info = $this->model->where('shop_id', $ids)->find();
            try {
                $params["left_num"] = (intval($params["limit_num"])-intval($shop_info["limit_num"]))+intval($shop_info["left_num"]);
                $params['paike_gere'] =  $order_paike_gere_bean;
                $params['paike_jingdan'] =  $order_paike_jingsha_bean;

                if($is_praise==1) {
                    $params['paike_geredan_praise_first'] = floatval($order_paike_first_reward);
                    $params['paike_geredan_praise_sec'] = floatval($order_paike_second_reward);
                }else{
                    $params['paike_geredan_praise_first'] = 0.00;
                    $params['paike_geredan_praise_sec'] = 0.00;
                }

                if($is_praise_jin==1){
                    $params['paike_jingdan_praise_first'] = floatval($order_paike_first_reward);
                    $params['paike_jingdan_praise_sec'] =  floatval($order_paike_second_reward);
                }else{
                    $params['paike_jingdan_praise_first'] = 0.00;
                    $params['paike_jingdan_praise_sec'] = 0.00;
                }

                $this->model->where('shop_id', $ids)->update($params);
                $this->success('修改成功');
            } catch (Exception $e) {
                $this->error('修改失败');
            }
        }
        $this->view->assign("min_limit",$min_limit);
        $this->view->assign("min_limit_jing",$min_limit_jing);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
